using AdminAuthority.Attributes;

using Abc.Application;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Web.Mvc.Areas.HtAdmin.Controllers
{
    /// <summary>
    ///
    /// </summary>
    [NonValidation]
    //[AuthorizePolicy]
    [AllowAnonymous]
    [AuthorityController(typeCode: "HtAdmin", controllerDescription: "角色")]
    public class RoleController : HtAdminBaseController
    {
        private readonly ILogger<RoleController> _logger;
        private readonly IRoleService _roleService;

        public RoleController(ILogger<RoleController> logger
        , IRoleService roleService)
        {
            _logger = logger;
            _roleService = roleService;
        }

        [HttpGet]
        [AuthorityPermission(name: "列表", actions: "Index,GetList")]
        public async Task<IActionResult> Index()
        {
            return View();
        }

        [HttpGet, HttpPost]
        public async Task<JsonResult> GetList(RoleQuery searchquery)
        {
            var pagedlist = await _roleService.GetAllByPageToLayui(searchquery);
            return Json(pagedlist);
        }

        [AuthorityPermission(name: "详情", actions: "Info")]
        public async Task<ActionResult> Info(int? id)
        {
            RoleOut entity = null;
            if (id.HasValue)
            {
                entity = await _roleService.Find(id.Value);
            }
            return View(entity ?? new RoleOut());
        }

        [HttpPost]
        // [ValidateInput(false)]
        [AuthorityPermission(name: "编辑", actions: "Edit")]
        public async Task<JsonResult> Edit(RoleInput input)
        {
            return Json(await _roleService.InsertOrUpdate(input));
        }

        [HttpPost]
        [AuthorityPermission(name: "删除", actions: "Delete")]
        public async Task<ActionResult> Delete(int id)
        {
            return Json(await _roleService.Delete(id));
        }

        [HttpPost]
        [AuthorityPermission(name: "设置状态", actions: "SetState")]
        public async Task<JsonResult> SetState(int id, bool state, string type)
        {
            var resultdata = await _roleService.SetState(id, state, type);
            return Json(resultdata);
        }

        /// <summary>
        /// 获取权限数据
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        [AuthorityPermission(name: "获取菜单权限数据", actions: "GetPermissionDtree")]
        public async Task<JsonResult> GetPermissionDtree(int roleid = 0)
        {
            var permissionDtree = await _roleService.GetRoleDtreeCheck(roleid);
            return Json(permissionDtree);
        }

        /// <summary>
        /// 设置角色权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AuthorityPermission(name: "设置角色权限", actions: "SetMenuPermission")]
        public async Task<JsonResult> SetMenuPermission(RoleInput input)
        {
            return Json(await _roleService.SetRolePermission(input));
        }
    }
}